var express = require("express");
const app = express();
var router = express.Router();
const axios = require('axios');
const tencentcloud = require('tencentcloud-sdk-nodejs');
const FmuClient = tencentcloud.fmu.v20191213.Client;

// 初始化客户端配置，替换为你的真实密钥信息
const clientConfig = {
  credential: {
    secretId: 'AKIDsb4GD0cy3uUoreBQO6Yjdw4zh9MIzQaV',
    secretKey: 'g6GAnAH8KJUVHn57xay74uc7OYbT6UbS'
  },
  region: 'ap-guangzhou', // 根据实际需求调整区域
  profile: {
    httpProfile: {
      endpoint: 'fmu.tencentcloudapi.com'
    }
  }
};

const client = new FmuClient(clientConfig);

router.post('/tryLipstick', async (req, res) => {
  // LipColorInfos: [{ "RGBA": { "R": 220, "G": 2, "B": 44, "A": 50 }, "ModelAlpha": 50 }],
  let Image = await convertImageUrlToBase64(req.body.imageUrl)
  const reqParams = {
    // 填充请求参数，例如图片 URL 或 Base64 编码的图片数据、唇色素材 ID 或 RGBA 值等
    // 参考文档确定准确的参数格式和名称
    Url:req.body.imageUrl,
    Image,
    LipColorInfos: req.body.LipColorInfos,
    RspImgType: 'url'
  };
  console.log(reqParams)

  try {
    const response = await client.TryLipstickPic(reqParams);
    // 处理响应数据，例如将处理后的图片返回给客户端或保存到服务器
    res.json({ code: 1, data: response });
  } catch (error) {
    res.json({ code: 0, msg: error });
  }

});

async function convertImageUrlToBase64(imageUrl) {
  return new Promise( async(resolve,reject)=>{
    try {
      const response = await axios.get(imageUrl, {
        responseType: 'arraybuffer'
      });
      const buffer = Buffer.from(response.data);
      resolve( buffer.toString('base64'));
    } catch (error) {
      reject(error)
    }
  })
 
}


module.exports = router;
